<?php

namespace app\admin\model\food;

use think\Model;


class Dish extends Model
{

    

    

    // 表名
    protected $name = 'food_dish';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'dish_type_text',
        'cook_time_text',
        'difficulty_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getDishTypeList()
    {
        return ['1' => __('Dish_type 1'), '2' => __('Dish_type 2'), '3' => __('Dish_type 3'), '4' => __('Dish_type 4'), '5' => __('Dish_type 5'), '6' => __('Dish_type 6')];
    }

    public function getDifficultyList()
    {
        return ['1' => __('Difficulty 1'), '2' => __('Difficulty 2'), '3' => __('Difficulty 3')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getDishTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['dish_type'] ?? '');
        $list = $this->getDishTypeList();
        return $list[$value] ?? '';
    }


    public function getCookTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['cook_time'] ?? '');
        return $value;
    }


    public function getDifficultyTextAttr($value, $data)
    {
        $value = $value ?: ($data['difficulty'] ?? '');
        $list = $this->getDifficultyList();
        return $list[$value] ?? '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    protected function setCookTimeAttr($value)
    {
        return $value === '' ? null : $value;
    }
    
    /**
     * 关联食材关系表
     */
    public function ingredientrels()
    {
        return $this->hasMany('Ingredientrel', 'dish_id', 'id');
    }


}